# coding: UTF-8

#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from enum import Enum
from typing import List

from pydantic import BaseModel, Field

from clickhouse.common.clickhouse_constants import SubJobType, ExecutePolicy


class CommonBodyResponse(BaseModel):
    code: int = Field(default=0, description="执行结果")
    body_err: int = Field(None, description="错误码", alias='bodyErr')
    message: str = Field(default='', description="错误信息")


class Node(BaseModel):
    id: str = Field(default=None, description='id')
    name: str = Field(default=None, description='name')
    type: str = Field(default=None, description='type')
    sub_type: str = Field(default=None, description='subType', alias='subType')
    endpoint: str = Field(default=None, description='endpoint')
    role: int = Field(default=1, description='role')
    extend_info: dict = Field(None, description='extend_info', alias='extendInfo')


class QueryClusterResponse(BaseModel):
    id: str = Field(None, description='id')
    name: str = Field(None, description='name')
    type: str = Field(None, description='type')
    sub_type: str = Field(default=None, description='subType', alias='subType')
    endpoint: str = Field(None, description='endpoint')
    role: int = Field(default=1, description='role')
    extend_info: dict = Field(None, description='extend_info', alias='extendInfo')
    nodes: List[Node] = Field(None, description='node info')


class Resource(BaseModel):
    id: str = Field(default=None, description='id')
    name: str = Field(default=None, description='name')
    type: str = Field(default=None, description='type')
    sub_type: str = Field(default=None, description='subType', alias='subType')
    parent_id: str = Field(default=None, description='parentId', alias='parentId')
    parent_name: str = Field(default=None, description='parentName', alias='parentName')
    extend_info: dict = Field(None, description='extend_info', alias='extendInfo')


class ListAppV2Response(BaseModel):
    resource_list: List[Resource] = Field(default=[], description='resource list', alias='resourceList')
    page_no: int = Field(default=0, description='页号', alias='pageNo')
    pages: int = Field(default=1, description='总页数')
    page_size: int = Field(default=200, description='页大小', alias='pageSize')
    total: int = Field(default=0, description='总大小')


class PermissionInfo(BaseModel):
    user: str = Field(default='root', description='user')
    group: str = Field(default='root', description='group')
    file_mode: str = Field(default='0700', description='mod', alias='fileMode')


class SubJob(BaseModel):
    job_id: str = Field(default='', description='任务ID', alias='jobId')
    sub_job_id: str = Field(default='', description='子任务ID', alias='subJobId')
    job_type: str = Field(default=SubJobType.BUSINESS_SUB_JOB, description='任务类型', alias='jobType')
    job_name: str = Field(default='', description='任务名', alias='jobName')
    job_priority: int = Field(default=1, description='任务优先级', alias='jobPriority')
    policy: int = Field(default=ExecutePolicy.FIXED_NODE, description='策略')
    ignore_failed: bool = Field(default=False, description='是否忽略失败结果', alias='ignoreFailed')
    exec_node_id: str = Field(default='', description='执行任务节点ID', alias='execNodeId')
    job_info: str = Field(default='', description='任务信息', alias='jobInfo')


class Database(BaseModel):
    uuid: str = Field(None, description='数据库UUID')
    name: str = Field(None, description='数据库名称')
    engine: str = Field(None, description='数据库引擎')


class Table(BaseModel):
    uuid: str = Field(None, description='表UUID')
    name: str = Field(None, description='表名称')
    engine: str = Field(None, description='表引擎')
    database: str = Field(None, description='所属数据库名称')
    create_table_query: str = Field(None, description='创建表语句')
    data_paths: str = Field(None, description='表数据在文件系统中的路径')
    disk_names: str = Field(None, description='表数据在磁盘名称')


class Disk(BaseModel):
    name: str = Field(None, description='服务器配置中的磁盘名称')
    path: str = Field(None, description='文件系统中挂载点的路径')


class Cluster(BaseModel):
    cluster: str = Field(None, description='集群名')
    shard_num: str = Field(None, description='集群中的分片数，从1开始')
    shard_weight: str = Field(None, description='写数据时该分片的相对权重')
    replica_num: str = Field(None, description='分片的副本数量，从1开始')
    host_name: str = Field(None, description='配置中指定的主机名')
    host_address: str = Field(None, description='从DNS获取的主机IP地址')
    port: str = Field(None, description='连接到服务器的端口')
    is_local: str = Field(None, description='是否本地')
    user: str = Field(None, description='连接到服务器的用户名')


class Replicas(BaseModel):
    table: str = Field(None, description='表名')
    engine: str = Field(None, description='表引擎名称')
    zookeeper_path: str = Field(None, description='在ZooKeeper中的表数据路径')
    replica_name: str = Field(None, description='ZooKeeper中的副本名称。同一表的不同副本具有不同的名称')
    database: str = Field(None, description='数据库名称')


class SqliteTable(BaseModel):
    uuid: str = Field(None, description='UUID')
    name: str = Field(None, description='name')
    type: str = Field(None, description='type')
    parent_path: str = Field(default='/', description='父节点路径')
    parent_uuid: str = Field(None, description='PARENT_UUID')
    size: str = Field(None, description='大小')
    create_time: str = Field(None, description='创建时间')
    modify_time: str = Field(None, description='修改时间')
    extend_info: str = Field(None, description='扩展信息')
    res_type: str = Field(None, description='RES_TYPE')
    res_sub_type: str = Field(None, description='RES_SUB_TYPE')